package com.ndkj.rpcserver.proxy;

import com.ndkj.rpcserver.proxy.chain.Handler;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 责任链模式，针对Consumer的代理对象执行逻辑进行解耦
 */
public class ProxyChainHandler  {

    private List<Handler> handlers = new ArrayList<>();

    public void addHandler(Handler handler) {
        this.handlers.add(handler);
    }

    public Object process(Class interfaceClazz, String version, Object proxy, Method method, Object[] args) throws IOException {
        for (Handler handler : this.handlers) {
            if (handler.canHandle()) {
                System.err.println("process by " + handler.getClass().getName());
                return handler.process(interfaceClazz, version, proxy, method, args);
            }
        }
        throw new RuntimeException("Could not handle the Request");
    }

}
